package com.yami.shop.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.MsgDto;
import com.yami.shop.bean.model.Msg;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.MsgService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;


/**
 * (Msg)表控制层
 *
 * @author rows
 * @since 2023-04-11 09:41:36
 */
@RestController
@RequestMapping("msg")
@RequiredArgsConstructor
@Slf4j
public class MsgController {
    /**
     * 服务对象
     */
    private final MsgService msgService;


    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param msg  查询实体
     * @return 分页数据
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_msg_view')")
    public ServerResponseEntity getMsgPage(Page<Msg> page, MsgDto msg) {
        return ServerResponseEntity.success(this.msgService.selectPage(page, msg));
    }


    /**
     * 通过id查询
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("{id}")
    @PreAuthorize("@pms.hasPermission('admin_msg_view')")
    public ServerResponseEntity getById(@PathVariable Serializable id) {
        return ServerResponseEntity.success(this.msgService.getById(id));
    }


    /**
     * 新增数据
     *
     * @param msg 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增", description = "新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_msg_add')")
    public ServerResponseEntity insert(@RequestBody Msg msg) {
        return ServerResponseEntity.success(this.msgService.save(msg));
    }


    /**
     * 修改
     *
     * @param msg
     * @return ServerResponseEntity
     */
    @Operation(summary = "修改", description = "修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_msg_edit')")
    public ServerResponseEntity updateById(@RequestBody Msg msg) {
        return ServerResponseEntity.success(this.msgService.updateById(msg));
    }


    /**
     * 通过id删除
     *
     * @param id id
     * @return ServerResponseEntity
     */
    @Operation(summary = "通过id删除", description = "通过id删除")
    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('admin_msg_del')")
    public ServerResponseEntity removeById(@PathVariable Serializable id) {
        this.msgService.remove(id);
        return ServerResponseEntity.success(true);
    }

}